package com.blacktry.controller.blog;


import com.blacktry.entity.enums.OprLogConst;
import com.blacktry.entity.model.ApiResult;
import com.blacktry.service.CommentsService;
import com.blacktry.utils.ApiResultHandler;
import com.blacktry.utils.log.OperLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author blackTry
 * @since 2022-02-07
 */
@Api(value = "评论模块", tags = "评论管理接口")
@RestController
@RequestMapping("/comments")
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    @OperLog(operModul = "评论管理-查询文章的评论", operType = OprLogConst.SELECT, operDesc = "查询文章的评论")
    @ApiOperation("查询文章的评论")
    @PostMapping("/selectCommentsByArticleId/{articleId}")
    public ApiResult<Map<Integer, List<Integer>>> selectCommentsByArticleId(@PathVariable("articleId") Integer articleId) {
        return ApiResultHandler.buildApiResult(200, "请求成功！", commentsService.selectCommentsByArticleId(articleId));
    }


    @OperLog(operModul = "评论管理-添加文章的评论", operType = OprLogConst.ADD, operDesc = "添加文章的评论")
    @ApiOperation("添加文章的评论")
    @PostMapping("/insertCommentsByArticleId/{articleId}")
    public ApiResult<Integer> insertCommentsByArticleId(@PathVariable("articleId") Integer articleId , Principal principal) {
        String userId=principal.getName();
        return ApiResultHandler.buildApiResult(200, "请求成功！", commentsService.insertCommentsByArticleId(articleId,userId));
    }

    @OperLog(operModul = "评论管理-删除文章的评论", operType = OprLogConst.ADD, operDesc = "删除文章的评论")
    @ApiOperation("删除文章的评论")
    @PostMapping("/deleteCommentsByCommentId/{commentId}")
    public ApiResult<Integer> deleteCommentsByCommentId(@PathVariable("commentId") Integer commentId ) {

        return ApiResultHandler.buildApiResult(200, "请求成功！", commentsService.deleteCommentsByCommentId(commentId));
    }
}

